//
//  ArticleBigImageTypeCell.m
//  Yanwei
//
//  Created by Seven-Augus on 2017/5/31.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "ArticleBigImageTypeCell.h"
/**
#import "ReadListConfigHeader.h"
#import "DispatchHTMLManager.h"
 */
#import "UIImage+Additions.h"

@interface ArticleBigImageTypeCell ()

@property (nonatomic, strong) UIView *cellBackgroundView;
@property (nonatomic, strong) UIImageView *cellArticleImageView; // 文章图片
@property (nonatomic, strong) UILabel *cellArticleLabel; // 文章描述
@property (nonatomic, strong) UILabel *cellArticleFromLabel; // 文章出处
@property (nonatomic, strong) UILabel *cellArticleFromAuthorNameLabel; // 文章作者名
@property (nonatomic, strong) UILabel *cellArticleWatchNumLabel; // 文章查看数
@property (nonatomic, strong) UIImageView *cellArticleWatchNumImageView; // 文章查看数图片

@property (strong, nonatomic) UILabel * selectLabel;//选择框
@property (nonatomic, strong) UILabel *cellArticlePublishTimeLabel; // 文章发布时间
@property (strong, nonatomic) UIView * bottomLineView;//底部线条

@property (nonatomic, strong) UIButton *imageStateDescribe;

@end

@implementation ArticleBigImageTypeCell

/**
+ (CGFloat)cellHeightWithArticleListDataModel:(ArticleListDataModel *)articleListDataModel indexPath:(NSIndexPath *)indexPath {
    //图片的高度＋图片与标题的间距＋标题距离背景视图底部的距离＋底部线条的高度
    CGFloat h = ScreenWidth * 0.56 + 28;
    // 描述
    CGSize maxSize = TextSize_MutiLine(@"高\n度", APPBOLDPINGFANGFONT(17), CGSizeMake(ScreenWidth - 30, 1000));
    CGSize size = TextSize_MutiLine(articleListDataModel.title, APPBOLDPINGFANGFONT(17), CGSizeMake(ScreenWidth - 30, maxSize.height));
    
    if (size.height > maxSize.height) {
        size = maxSize;
    }
    h += size.height;
    
    // 来源
    CGFloat height = [@"来源" boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(10)} context:nil].size.height;
    h += height;
    if (indexPath.row == 0) {
        
    } else {
        CGFloat kMagnSpace = (CGFloat)(0.5);
        h += kMagnSpace;
    }
    
    return ceilf(h);
}
 */

+ (instancetype)initTableView:(UITableView *)tableView {
    ArticleBigImageTypeCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ArticleBigImageTypeCell class])];
    if (!cell) {
        cell = [[ArticleBigImageTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([ArticleBigImageTypeCell class])];
    }
    
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.separatorInset = UIEdgeInsetsMake(0, ScreenWidth, 0, 0);
        [self initView];
    }
    return self;
}

/** 初始化视图 */
- (void)initView {
    self.cellBackgroundView.backgroundColor = [UIColor whiteColor];
    [self.cellArticleImageView setImage:NewStoreDefault];
    self.cellArticleLabel.text = @"她是最年轻的金像奖影后最有灵气的文艺女神";
    [UILabel changeLineSpaceForLabel:self.cellArticleLabel WithSpace:8.0];
    self.cellArticleFromLabel.text = [@"网易新闻" stringByAppendingString:@" | "];
    self.cellArticleFromAuthorNameLabel.text = @"张萌萌";
    
    self.cellArticleWatchNumLabel.text = @"123";
    self.cellArticleWatchNumImageView.backgroundColor = [UIColor clearColor];
}


#pragma mark - setting
/**
- (void)setArticleListDataModel:(ArticleListDataModel *)articleListDataModel {
    _articleListDataModel = articleListDataModel;
    
    self.bottomLineView.hidden = YES;
    if (self.indexPath.row == 0) {
        [self.cellBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
        }];
    } else {
        CGFloat kMagnSpace = (CGFloat)(0.5);
        [self.cellBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).with.offset(kMagnSpace);
        }];
        self.bottomLineView.hidden = NO;
    }
    [self.cellArticleImageView setNeedsLayout];
    [self.cellArticleImageView layoutIfNeeded];
    
    NSArray *imageUrlArr = [articleListDataModel.titleImg componentsSeparatedByString:@"-"];
    NSString *bigImageUrl = [imageUrlArr.firstObject stringByAppendingString:@"-w1080"];
    
    [self loadImageWithOriginalImage:PacketImageUrl(bigImageUrl) thumbnailImage:PacketImageUrl(bigImageUrl) imageView:self.cellArticleImageView];
    self.cellArticleLabel.text = _articleListDataModel.title;
    switch (_articleListDataModel.copyright) {
        case SearchArticleOriginalType: {
            self.cellArticleFromLabel.text = @"";
            self.cellArticleFromAuthorNameLabel.text = [NSString stringWithFormat:@"%@", articleListDataModel.userName];
            
            [self.cellArticleFromAuthorNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.cellArticleFromLabel.mas_right);
            }];
            break;
        }
        case SearchArticleeRprintedType: {
            self.cellArticleFromLabel.text = @"";
            self.cellArticleFromAuthorNameLabel.text = [NSString stringWithFormat:@"%@", articleListDataModel.origin];
            
            [self.cellArticleFromAuthorNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.cellArticleFromLabel.mas_right);
            }];
            break;
        }
        default:
            break;
    }
    
    self.cellArticleWatchNumLabel.text = [NSString stringWithFormat:@"%@阅读", _articleListDataModel.readCount];
    self.cellArticleWatchNumImageView.backgroundColor = [UIColor clearColor];
    self.cellArticlePublishTimeLabel.text = articleListDataModel.detailTimeStr;
    
    [UILabel changeLineSpaceForLabel:self.cellArticleLabel WithSpace:2.5];
    
    if (articleListDataModel.hasSelected) {
        self.selectLabel.textColor = Color175030;
        self.selectLabel.text = SELECT_D;
    } else {
        self.selectLabel.textColor = HEXCOLOR(0xcacaca);
        self.selectLabel.text = SELECT_N;
    }
    
    self.imageStateDescribe.hidden = YES;
    if (articleListDataModel.isHomeUser && articleListDataModel.isPersonHome) {
        self.imageStateDescribe.hidden = NO;
    }
}
- (void)changeTitleStatus {
    [self.articleListDataModel keepReaded];
    self.cellArticleLabel.textColor = ReadTitleTextColor(YES);
}
- (void)displayReaded {
    if ([self.articleListDataModel isReaded]) {
        self.cellArticleLabel.textColor = ReadTitleTextColor(YES);
    } else {
        self.cellArticleLabel.textColor = Color34;
    }
}
- (void)setEditAction:(BOOL)editAction {
    _editAction = editAction;
    self.selectLabel.hidden = !editAction;
    if (editAction) {
        [self.cellBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).with.offset(30);
        }];
    } else {
        [self.cellBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
        }];
    }
}
 */


#pragma mark - 懒加载
- (UIView *)cellBackgroundView {
    if (!_cellBackgroundView) {
        _cellBackgroundView = [UIView new];
        _cellBackgroundView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_cellBackgroundView];
        
        [_cellBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView);
            make.width.equalTo(self.contentView);
        }];
    }
    return _cellBackgroundView;
}
- (UIImageView *)cellArticleImageView {
    if (!_cellArticleImageView) {
        _cellArticleImageView = [UIImageView new];
        _cellArticleImageView.clipsToBounds = YES;
        [self.cellBackgroundView addSubview:_cellArticleImageView];
        [_cellArticleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.cellArticleLabel);
            make.top.equalTo(self.cellArticleLabel.mas_bottom).offset(8);
            make.height.equalTo(self.cellArticleImageView.mas_width).multipliedBy(0.56);
        }];
    }
    return _cellArticleImageView;
}
- (UILabel *)cellArticleLabel {
    if (!_cellArticleLabel) {
        _cellArticleLabel = [UILabel new];
        _cellArticleLabel.numberOfLines = 2;
        _cellArticleLabel.font = APPFONT(17);
        _cellArticleLabel.textColor = Color34;
        [self.cellBackgroundView addSubview:_cellArticleLabel];
        [_cellArticleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(self.cellBackgroundView).offset(15);
            make.right.equalTo(self.cellBackgroundView).offset(-15);
        }];
    }
    return _cellArticleLabel;
}
- (UILabel *)cellArticleFromLabel {
    if (!_cellArticleFromLabel) {
        _cellArticleFromLabel = [UILabel new];
        _cellArticleFromLabel.textColor = RGBACOLOR(0, 108, 183, 1);
        _cellArticleFromLabel.backgroundColor = RGBACOLOR(225, 243, 255, 1);
        _cellArticleFromLabel.cornerRadius = 2;
        _cellArticleFromLabel.font = APPFONT(10);
        [self.cellBackgroundView addSubview:_cellArticleFromLabel];
        [_cellArticleFromLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.cellArticleLabel);
            make.bottom.equalTo(self.cellBackgroundView).with.offset(-10);
        }];
    }
    return _cellArticleFromLabel;
}
- (UILabel *)cellArticleFromAuthorNameLabel {
    if (!_cellArticleFromAuthorNameLabel) {
        _cellArticleFromAuthorNameLabel = [UILabel new];
        _cellArticleFromAuthorNameLabel.textColor = Color140;
        _cellArticleFromAuthorNameLabel.font = APPFONT(10);
        [self.cellBackgroundView addSubview:_cellArticleFromAuthorNameLabel];
        [_cellArticleFromAuthorNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.cellArticleFromLabel.mas_right).with.offset(5);
            make.bottom.equalTo(self.cellArticleFromLabel);
        }];
    }
    return _cellArticleFromAuthorNameLabel;
}
- (UILabel *)cellArticleWatchNumLabel {
    if (!_cellArticleWatchNumLabel) {
        _cellArticleWatchNumLabel = [UILabel new];
        _cellArticleWatchNumLabel.textColor = Color140;
        _cellArticleWatchNumLabel.font = APPFONT(10);
        [self.cellBackgroundView addSubview:_cellArticleWatchNumLabel];
        [_cellArticleWatchNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.cellArticleFromAuthorNameLabel.mas_right).with.offset(5);
            make.bottom.equalTo(self.cellArticleFromAuthorNameLabel);
        }];
    }
    return _cellArticleWatchNumLabel;
}
- (UILabel *)cellArticlePublishTimeLabel {
    if (!_cellArticlePublishTimeLabel) {
        _cellArticlePublishTimeLabel = [UILabel new];
        _cellArticlePublishTimeLabel.textColor = Color140;
        _cellArticlePublishTimeLabel.font = APPFONT(10);
        [self.cellBackgroundView addSubview:_cellArticlePublishTimeLabel];
        [_cellArticlePublishTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.cellArticleWatchNumLabel.mas_right).with.offset(5);
            make.bottom.equalTo(self.cellArticleFromLabel);
        }];
    }
    return _cellArticlePublishTimeLabel;
}
- (UILabel *)selectLabel {
    if (!_selectLabel) {
        _selectLabel = [UILabel new];
        _selectLabel.font = ICONFONT(15);
        _selectLabel.textColor = HEXCOLOR(0xcacaca);
        _selectLabel.userInteractionEnabled = NO;
        _selectLabel.hidden = YES;
        [self.contentView addSubview:_selectLabel];
        [_selectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(7);
            make.centerY.equalTo(self.contentView);
            make.width.and.height.mas_equalTo(17);
        }];
        
        __weakConfigure
        [_selectLabel tapUpWithBlock:^(UIView * v) {
            if (__weakSelf.clickedCheckBlock) {
                /**
                __weakSelf.articleListDataModel.hasSelected = !__weakSelf.articleListDataModel.hasSelected;
                 **/
                __weakSelf.clickedCheckBlock(__weakSelf.indexPath);
            }
        }];
    }
    return _selectLabel;
}

- (UIView *)bottomLineView {
    if (!_bottomLineView) {
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = Color217;
        [self.contentView addSubview:_bottomLineView];
        [_bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(15);
            make.right.equalTo(self.contentView).offset(-15);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _bottomLineView;
}

- (UIButton *)imageStateDescribe {
    if (!_imageStateDescribe) {
        _imageStateDescribe = [UIButton buttonWithType:UIButtonTypeSystem];
        UIImage *imageStateDescribeImg = [UIImage imageNamed:@"imageStateDescribe"];
        imageStateDescribeImg = [imageStateDescribeImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [_imageStateDescribe setImage:imageStateDescribeImg forState:UIControlStateNormal];
        _imageStateDescribe.userInteractionEnabled = NO;
        [self.cellArticleImageView addSubview:_imageStateDescribe];
        [_imageStateDescribe mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.cellArticleImageView).with.offset(6);
            make.right.equalTo(self.cellArticleImageView).with.offset(- 5);
            make.width.mas_equalTo(@(40));
            make.height.mas_equalTo(@(17));
        }];
    }
    return _imageStateDescribe;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)reFreshNightModelLayout {
    /**
    BOOL selected = [DispatchHTMLManager checkRecordDidSelectedWithModel:(JSONModel *)self.articleListDataModel];
    self.cellArticleFromAuthorNameLabel.textColor = ReadUserNameTextColor(selected);
    self.cellArticleWatchNumLabel.textColor = ReadCommentTextColor(selected);
    self.cellArticleLabel.textColor = ReadTitleTextColor(selected);
    self.cellArticleFromLabel.textColor = ReadUserNameTextColor(selected);
    */
}

@end
